#include <QtGui>
#include "helper.h"
#include <QMessageBox>

KHelper::KHelper(QWidget *parent) : QWidget(parent)
{
	m_systemTrayIcon = new QSystemTrayIcon;

	m_brightness = new KBrightness;
	m_restReminder = new KRestReminder(m_systemTrayIcon);

	m_restReminder->start();
	initGui();
	setAttribute(Qt::WA_StaticContents);
	setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

	qApp->setStyle(new KHelperStyle);

	qDebug() << "class name: " << this->metaObject()->className();

	// QObject *object = this;
	// const QMetaObject *metaObject = object->metaObject();
	// int count = metaObject->propertyCount();
	// for (int i = 0; i < count; i++) {
	// 	QMetaProperty metaproperty = metaObject->property(i);
	// 	const char *name = metaproperty.name();
	// 	QVariant value = object->property(name);
	// 	qDebug() << value;
	// }

}

KHelper::~KHelper()
{
}

void KHelper::initGui()
{
	// set origin brightness
	m_originBrightness = getBrightness();

	m_settings = new QSettings("Kingsoft", "wpshelper");
	m_originWorkAmountTime = m_settings->value("time/Work_Amount_Time", DEFAULT_WORK_AMOUNT_TIME).toInt();
		
	// system tray icon
	QIcon icon(":/images/wpshelper_icon.svg");
	m_systemTrayIcon->setToolTip(QApplication::applicationName());
	m_systemTrayIcon->setIcon(icon);
	m_systemTrayIcon->show();

	connect(m_systemTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
	setWindowIcon(icon);

	// setting wizard
	/* brightness */
	m_brightnessGroupBox = new QGroupBox(tr("Change Brightness:"));
	m_brightnessSlider = new QSlider(Qt::Horizontal);
	m_brightnessSpinBox = new QSpinBox;
	m_brightnessUndoBtn = new QPushButton;
	m_brightnessBoxLayout = new QHBoxLayout;
	m_brightnessValueLabel = new QLabel(QString("%1\%").arg(m_originBrightness));
	
	m_brightnessSlider->setRange(0, 100);
	m_brightnessSpinBox->setRange(0, 100);
	m_brightnessSlider->setValue(m_originBrightness);

	connect(m_brightnessSlider, SIGNAL(valueChanged(int)), this, SLOT(setBrightness(int)));

	QPixmap brightnessUndoBtnPixmap(":/images/undo.svg");
	QIcon brightnessUndoBtnIcon(brightnessUndoBtnPixmap);
	m_brightnessUndoBtn->setIcon(brightnessUndoBtnIcon);
	m_brightnessUndoBtn->setIconSize(QSize(15, 15));
	m_brightnessUndoBtn->setToolTip(tr("Undo"));
//	m_brightnessUndoBtn->setFlat(true);

	m_brightnessUndoBtn->setEnabled(false);
	connect(m_brightnessUndoBtn, SIGNAL(clicked()), this, SLOT(restoreBrightness()));

	m_brightnessBoxLayout->addWidget(m_brightnessSlider);
	m_brightnessBoxLayout->addWidget(m_brightnessValueLabel);
	m_brightnessBoxLayout->addWidget(m_brightnessUndoBtn);
	m_brightnessGroupBox->setLayout(m_brightnessBoxLayout);
				
	/* rest reminder */
	m_restReminderGroupBox = new QGroupBox(tr("Rest Reminder Settings: "));
	m_restReminderBoxLayout = new QHBoxLayout;
	m_restReminderSpinBox = new QSpinBox;
	m_restReminderApplyBtn = new QPushButton;
	m_restReminderUndoBtn = new QPushButton;
	
	m_restReminderSpinBox->setRange(1, 300);
	m_restReminderSpinBox->setValue(m_originWorkAmountTime);

	m_restReminderApplyBtn->setEnabled(false);
	QPixmap rrApplyBtnPixmap(":/images/apply.svg");  // rr is short for rest reminder
	QIcon rrApplyBtnIcon(rrApplyBtnPixmap);
	m_restReminderApplyBtn->setIcon(rrApplyBtnIcon);
	m_restReminderApplyBtn->setIconSize(QSize(15, 15));
	m_restReminderApplyBtn->setToolTip(tr("Apply changes to settings"));
//	m_restReminderApplyBtn->setFlat(true);
	m_restReminderApplyBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

	m_restReminderUndoBtn->setEnabled(false);
	QPixmap rrUndoBtnPixmap(":/images/undo.svg");  // rr is short for rest reminder
	QIcon rrUndoBtnIcon(rrUndoBtnPixmap);
	m_restReminderUndoBtn->setIcon(rrUndoBtnIcon);
	m_restReminderUndoBtn->setIconSize(QSize(15, 15));
	m_restReminderUndoBtn->setToolTip(tr("Undo"));
//	m_restReminderUndoBtn->setFlat(true);
	m_restReminderUndoBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	
	connect(m_restReminderSpinBox, SIGNAL(valueChanged(int)), this, SLOT(enableRRApplyUndoBtn()));
	connect(m_restReminderApplyBtn, SIGNAL(clicked()), this, SLOT(applyRRSettings()));
	connect(m_restReminderUndoBtn, SIGNAL(clicked()), this, SLOT(undoRRSettings()));
	
	m_restReminderBoxLayout->addWidget(new QLabel(tr("Current remind timeout is: ")));
	m_restReminderBoxLayout->addWidget(m_restReminderSpinBox);
	m_restReminderBoxLayout->addWidget(new QLabel(tr("minutes")));
	m_restReminderBoxLayout->addWidget(m_restReminderUndoBtn);
	m_restReminderBoxLayout->addWidget(m_restReminderApplyBtn);
	m_restReminderGroupBox->setLayout(m_restReminderBoxLayout);

	/* menu actions */
	m_systemTrayIconMenu = new QMenu;
	m_systemTrayIconMenu->addAction(tr("Mini&mized"), this, SLOT(hide()));
	m_systemTrayIconMenu->addAction(tr("Resto&re"), this, SLOT(show()));
	m_systemTrayIconMenu->addSeparator();
	m_systemTrayIconMenu->addAction(QIcon(":/images/exit.png"), tr("E&xit"), qApp, SLOT(quit()));
	m_systemTrayIcon->setContextMenu(m_systemTrayIconMenu);

	/* main layout */
	m_mainLayout = new QVBoxLayout;
	m_mainLayout->addWidget(m_brightnessGroupBox);
	m_mainLayout->addWidget(m_restReminderGroupBox);

	m_mainLayout->setAlignment(Qt::AlignRight);
	setLayout(m_mainLayout);

	setWindowTitle(QApplication::applicationName());
	setMinimumSize(400, 150);

	setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
}


void KHelper::setBrightness(int value)
{
	m_brightnessValueLabel->setText(QString("%1\%").arg(value));
	m_brightnessUndoBtn->setEnabled(true);
	m_brightness->setBrightness(value);
}

int KHelper::getBrightness()
{
    return m_brightness->getBrightness();
}


void KHelper::enableRRApplyUndoBtn()
{
	m_restReminderApplyBtn->setEnabled(true);
	m_restReminderUndoBtn->setEnabled(true);
}

void KHelper::applyRRSettings()
{
	int workAmountTime = m_restReminderSpinBox->value();
	m_restReminder->setWorkAmountTime(workAmountTime);
	m_settings->setValue("time/Work_Amount_Time", workAmountTime);
	m_restReminder->restart();
	m_restReminderApplyBtn->setEnabled(false);
}

void KHelper::undoRRSettings()
{
	m_restReminderSpinBox->setValue(m_originWorkAmountTime);
	m_restReminderUndoBtn->setEnabled(false);
}

void KHelper::restoreBrightness()
{
	qDebug() << "current brightness: " << getBrightness() << " origin brightness: " << m_originBrightness;
	if (getBrightness() != m_originBrightness)
	{
		setBrightness(m_originBrightness);
		m_brightnessSlider->setValue(m_originBrightness);
	}
	m_brightnessUndoBtn->setEnabled(false);
	m_brightnessValueLabel->setText(QString("%1\%").arg(m_originBrightness));
}

void KHelper::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
{
	if (reason == QSystemTrayIcon::Trigger)
	{
		if (this->isVisible())
		{
			hide();
		} else {
			show();
		}
	}
}



